package com.jm.springboot21.controller;

import com.jm.springboot21.pojo.Expr;
import com.jm.springboot21.pojo.Result;
import com.jm.springboot21.service.ExprService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/expr")
@CrossOrigin("*")
public class ExprController {
    private final ExprService exprService;

    public ExprController(ExprService exprService) {
        this.exprService = exprService;
    }

    @PostMapping
    public Result addExpr(@RequestBody Expr expr) {
        exprService.insert(expr);
        return Result.success();
    }

    @PutMapping
    public Result updateExpr(@RequestBody Expr expr) {
        exprService.update(expr);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteExpr(@PathVariable Integer id) {
        exprService.delete(id);
        return Result.success();
    }

    @GetMapping("/emp/{empId}")
    public Result getExprsByEmpId(@PathVariable Integer empId) {
        List<Expr> exprs = exprService.findByEmpId(empId);
        return Result.success(exprs);
    }

    @GetMapping("/{id}")
    public Result getExprById(@PathVariable Integer id) {
        Expr expr = exprService.findById(id);
        return Result.success(expr);
    }
    @PostMapping("/batch")
    public Result addExprs(@RequestBody List<Expr> exprs) {
        exprService.addExprs(exprs);
        return Result.success();
    }
}
